

function mergeSort(arr) {  //采用自上而下的递归方法
    var len = arr.length;
    if(len < 2) {
        return arr;
    }
    var middle = Math.floor(len / 2),
        left = arr.slice(0, middle),
        right = arr.slice(middle);
        // console.log(left,right, )
        return merge(mergeSort(left), mergeSort(right));
}   

function merge(left, right)
{
    console.log(left, right, 11111)
    var result = [];
    while (left.length>0 && right.length>0) {
        if (left[0] <= right[0]) {
            result.push(left.shift());
        } else {
            result.push(right.shift());
        }
    }

    while (left.length)
        result.push(left.shift());

    while (right.length)
        result.push(right.shift());
    console.log('result'+result)
    return result;
}
let data = [5, 6, 3, 1, 8, 7, 2, 4]
mergeSort(data)